package com.example.weektwo.async02;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ListView;

import com.example.weektwo.R;
import com.example.weektwo.Utils.LoadNetDataAsyncTask;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class VideoPlarerActivity extends AppCompatActivity implements LoadNetDataAsyncTask.onGetNetDataListener{
    ListView videoLv;
    List<VideoInfo.ItemListBean> mDatas;
    private VideoAdapter videoAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_plarer);
        videoLv=findViewById(R.id.video_lv);
        String url = "http://baobab.kaiyanapp.com/api/v4/tabs/selected?udid=11111&vc=168&vn=3.3.1&deviceModel=Huawei6&first_channel=eyepetizer_baidu_market&last_channel=eyepetizer_baidu_market&system_version_code=20";
        //启动异步任务
        mDatas=new ArrayList<>();
        videoAdapter = new VideoAdapter(this, mDatas);
        videoLv.setAdapter(videoAdapter);
        LoadNetDataAsyncTask task=new LoadNetDataAsyncTask(this, (LoadNetDataAsyncTask.onGetNetDataListener) this);
        task.execute(url);

    }

    private void parseData(String s) {
        VideoInfo videoInfo = new Gson().fromJson(s, VideoInfo.class);
        List<VideoInfo.ItemListBean> list = videoInfo.getItemList();
        //遍历 将type=video添加
        for (int i = 0; i < list.size(); i++) {
            VideoInfo.ItemListBean itemBean = list.get(i);
            String type = itemBean.getType();
            if (type.equals("video")) {
                mDatas.add(itemBean);
            }
        }
        videoAdapter.notifyDataSetChanged();
    }

    @Override
    public void showDataFromNet(String s) {
        parseData(s);
    }
}
